//
//  LoginAPI.m
//  RainbowFM
//
//  Created by chenli on 14/12/16.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "LoginAPI.h"
#import "AFAppDotNetAPIClient.h"

@implementation LoginAPI

-(void)fetchSecurityCodeWithPhoneNum:(NSString *)phoneNum result:(void (^)(BOOL,NSString *))res{
    [[AFAppDotNetAPIClient sharedClient] POST:AAM_BASE_URL
                                   parameters:@{@"serviceName" : @"aam_user_reg_sms_apply_req",
                                                @"callType" : @"002",
                                                @"params" : @{@"userTel" : phoneNum}}
                                      success:^(NSURLSessionDataTask *task, id responseObject) {
                                          if ([responseObject[@"returnCode"] isEqualToString:@"000000"]) {
                                              res(YES,responseObject[@"result"][@"succMsg"]);
                                          }
                                          else{
                                              res(NO,responseObject[@"errorMsg"]);
                                          }
                                      } failure:^(NSURLSessionDataTask *task, NSError *error) {
                                          res(NO,@"网络错误");
                                      }];
}


-(void)verifyPhoneNumAndCode:(NSString *)code phoneNum:(NSString *)phoneNum result:(void (^)(NSString *,NSString *))res{
    [[AFAppDotNetAPIClient sharedClient] POST:AAM_BASE_URL
                                   parameters:@{@"serviceName" : @"aam_user_reg_sms_validate_req",
                                                @"callType" : @"002",
                                                @"params" : @{@"userTel" : phoneNum,
                                                              @"smsCode" : code}}
                                      success:^(NSURLSessionDataTask *task, id responseObject) {
                                          if ([responseObject[@"returnCode"] isEqualToString:@"000000"]) {
                                              res(responseObject[@"result"][@"smsValidateToken"],responseObject[@"result"][@"succMsg"]);
                                          }
                                          else{
                                              res(nil,responseObject[@"errorMsg"]);
                                          }
                                      } failure:^(NSURLSessionDataTask *task, NSError *error) {
                                          res(NO,@"网络错误");
                                      }];
}

-(void)registerWithPhoneNum:(NSString *)phoneNum smsCode:(NSString *)code password:(NSString *)pwd result:(void (^)(BOOL,NSString *))res{
    [[AFAppDotNetAPIClient sharedClient] POST:AAM_BASE_URL
                                   parameters:@{@"serviceName" : @"aam_user_fast_reg_req",
                                                @"callType" : @"002",
                                                @"params" : @{@"userTel" : phoneNum,
                                                              @"userPwd" : pwd,
                                                              @"smsValidateToken" : code}}
                                      success:^(NSURLSessionDataTask *task, id responseObject) {
                                          if ([responseObject[@"returnCode"] isEqualToString:@"000000"]) {
                                              res(YES,responseObject[@"result"][@"succMsg"]);
                                          }
                                          else{
                                              res(NO,responseObject[@"errorMsg"]);
                                          }
                                      } failure:^(NSURLSessionDataTask *task, NSError *error) {
                                          
                                      }];
}


-(void)loginWithPhoneNum:(NSString *)phoneNum password:(NSString *)pwd result:(void (^)(NSString *, NSString *, NSString *))res{
    [[AFAppDotNetAPIClient sharedClient] POST:AAM_BASE_URL
                                   parameters:@{@"serviceName" : @"aam_user_login_req",
                                                @"callType" : @"002",
                                                @"params" : @{@"userTel" : phoneNum,
                                                              @"userPwd" : pwd}}
                                      success:^(NSURLSessionDataTask *task, id responseObject) {
                                          if ([responseObject[@"returnCode"] isEqualToString:@"000000"]) {
                                              res(responseObject[@"result"][@"tokenId"],responseObject[@"result"][@"userId"],responseObject[@"result"][@"succMsg"]);
                                          }
                                          else{
                                              res(nil,nil,responseObject[@"errorMsg"]);
                                          }
                                      } failure:^(NSURLSessionDataTask *task, NSError *error) {
                                          
                                      }];
}

-(void)loginOutWithUserID:(NSString *)userId result:(void (^)(BOOL))res{
    [[AFAppDotNetAPIClient sharedClient] POST:AAM_BASE_URL
                                   parameters:@{@"serviceName" : @"aam_user_logout_req",
                                                @"callType" : @"002",
                                                @"params" : @{@"userId" : userId}}
                                      success:^(NSURLSessionDataTask *task, id responseObject) {
                                          if ([responseObject[@"returnCode"] isEqualToString:@"000000"]) {
                                              res(YES);
                                          }
                                          else{
                                              res(NO);
                                          }
                                      } failure:^(NSURLSessionDataTask *task, NSError *error) {
                                          
                                      }];
}

@end
